home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 262_01.zip / WFC.C < prev   
Text File  |  1993-04-14  |  4KB  |  103 lines

  1. /****************************************************************
  2.  *               Word Frequency Analyzer Program                *
  3.  *          to demonstrate use of symbol table library          *
  4.  *                                                              *
  5.  *             Robert Ramey    25 September 1986                *
  6.  ****************************************************************/
  7.  
  8. /****************************************************************
  9. This program processes one or more text file.   When all files
  10. have been processed,  a table is produced on the standard output
  11. which shows how many times each word in the text occurred
  12. *****************************************************************/
  13. #include <stdio.h>
  14.  
  15. typedef char SYMBOLTABLE;
  16. main(argc,argv)
  17. int argc;
  18. char *argv[];
  19. {
  20.     FILE *file_pointer;
  21.     SYMBOLTABLE *symbol_table_pointer;
  22.     char *symbol_pointer;
  23.     int i;
  24.  
  25.     /* initialize symbol table.  Data is one integer per symbol
  26.     to contain the count of the occurrences of that symbol in
  27.     the text.  Hash is set to 256 as it seems to be on the order
  28.     of the number of different words in a short text. */
  29.     symbol_table_pointer = symmk(sizeof(int),256);
  30.  
  31.     if(argc == 1){
  32.         /* if called without arguments assume standard input*/
  33.         file_pointer = stdin;
  34.         ldwords(symbol_table_pointer,stdin);
  35.     }
  36.     else
  37.         /* for each argument process corresponding file */
  38.         for(i = 1;i < argc;++i)
  39.             if(file_pointer = fopen(argv[i],"r"))
  40.             ldwords(symbol_table_pointer,file_pointer);
  41.  
  42.     /* dump results to output */
  43.     i = 0;
  44.     while(symbol_pointer = symdmp(symbol_table_pointer, i++))
  45.         printf("%d\t%s\n",
  46.         *(int *)symdat(symbol_table_pointer,symbol_pointer),
  47.         symbol_pointer);
  48.     exit(0);
  49. }
  50. /*************************************************************
  51. ldwords - reads one file and loads words to symbol table
  52. ***********************************************************/
  53. void
  54. ldwords(symbol_table_pointer,file_pointer)
  55. FILE *file_pointer;
  56. SYMBOLTABLE *symbol_table_pointer;
  57. {
  58.     char *word_pointer, *symbol_pointer, *getwrd(),word_array[20];
  59.     while(word_pointer = getwrd(file_pointer,word_array))
  60.         if(symbol_pointer =
  61.         symlkup(symbol_table_pointer,word_pointer))
  62.             ++*((int *)symdat
  63.             (symbol_table_pointer,symbol_pointer));
  64.         else
  65.             if(symbol_pointer =
  66.             symadd(symbol_table_pointer,word_pointer) )
  67.                 *(int *)symdat
  68.                 (symbol_table_pointer,symbol_pointer)
  69.                 = 1;
  70.             else{
  71.                 printf("Sorry - out of memory\n");
  72.                 exit(1);
  73.             }
  74.     return;
  75. }
  76. /****************************************************************
  77. getwrd - returns pointer to character string equal to next word
  78. in file.  Punctuation is considered white space.  Returned words
  79. contain no white space.
  80. ****************************************************************/
  81. char *
  82. getwrd(file_pointer,word_pointer)
  83. FILE *file_pointer;
  84. char *word_pointer;
  85. {
  86.     char *wp, character;
  87.  
  88.     wp = word_pointer;
  89.     /* skip to next letter */
  90.     while(!isalpha(character))
  91.         if((character = getc(file_pointer)) == EOF)
  92.             return NULL;
  93.  
  94.     /* gather characters of word */
  95.     while(isalnum(character)){
  96.         *wp++ = character;
  97.         character = getc(file_pointer);
  98.     }
  99.  
  100.     *wp = '\0';
  101.     return word_pointer;
  102. }
  103. *****************************